/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.bitaka.ws.DBTransaction;
import dao.BilletDAO;
import dao.EvenementDAO;
import dao.GuichetDAO;
import dao.KitDAO;
import dao.MontantCaisseDAO;
import dao.OnligneDAO;
import dao.TransDAO;
import dao.TypeBilletDAO;
import dao.TypebDAO;
import dao.VenteDAO;
import entity.Billet;
import entity.Evenement;
import entity.Guichet;
import entity.Kit;
import entity.MontantCaisse;
import entity.Onligne;
import entity.Trans;
import entity.Trans_1;
import entity.Typeb;
import entity.Vente;
import java.sql.PreparedStatement;
import java.sql.Time;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.jboss.weld.context.ejb.Ejb;

/**
 *
 * @author Administrateur
 */
@ManagedBean(name = "annulation")
@SessionScoped
public class Annulation {

    List<Billet> lists = new ArrayList<Billet>();
    List<Trans_1> ventes = new ArrayList<Trans_1>();
    List<Kit> kits = new ArrayList<Kit>();
    @Ejb
    KitDAO kitDAO = new KitDAO();
    @Ejb
    BilletDAO billetDAO = new BilletDAO();
    @Ejb
    VenteDAO venteDAO = new VenteDAO();
    @Ejb
    TypebDAO typebDAO = new TypebDAO();
    OnligneDAO onligneDAO = new OnligneDAO();
    List<Typeb> events;
    int id_event;
    int idorder;

    public int getId_event() {
        return id_event;
    }
    List<Onligne> onlignes;

    public List<Onligne> getOnlignes() {
        return onlignes;
    }

    public int getIdorder() {
        return idorder;
    }

    public void setIdorder(int idorder) {
        this.idorder = idorder;
    }
    DBTransaction bTransaction = new DBTransaction();

    public void desac() throws ParseException {
        System.out.println("i=" + onlignes.size());
        for (int i = 0; i < onlignes.size(); i++) {
            if(!onlignes.get(i).isUtilise())
            {Trans trans_1 = new Trans();
            onligneDAO.beginTransaction();
            onlignes.get(i).setUtilise(true);
            trans_1.setDate(new Date());
            trans_1.setTime(new Date());
            trans_1.setPorte("Public");
            trans_1.setEtat("OK;Online");
            trans_1.setNumero(onlignes.get(i).getId().getNumeroSerie());
            bTransaction.insertTrans(trans_1);
            onligneDAO.merge(onlignes.get(0));
            onligneDAO.commit();
            }
            }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Billets desactivées ", ""));
    }

    public void setOnlignes(List<Onligne> onlignes) {
        this.onlignes = onlignes;
    }

    public void chercherOnligne() {
        if (idorder == 0) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Verifier IdOrder ", ""));

        } else {
            System.out.println("Enter ");
            onlignes = onligneDAO.findByIdOrder(idorder);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Liste affichées ", ""));

        }
    }

    public void setId_event(int id_event) {
        this.id_event = id_event;
    }
    Onligne o;
    Billet billet;

    public List<Typeb> getEvents() {
        return typebDAO.findByTypeWeb();
    }

    public void setEvents(List<Typeb> events) {
        this.events = events;
    }

    public Annulation() {
    }
    String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    TransDAO transDAO = new TransDAO();

    public void chercher() {
        if (id.length() == 0) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Numero Vide", ""));

        } else {
            o = new Onligne();
            billet = new Billet();
            if (id.contains("OL")) {
                ventes = transDAO.findByNumero(id);
                try {
                    o = (Onligne) onligneDAO.findByNemuro(id).get(0);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Recherche effectuée numero Billet Onligne:" + id + "\n Nombre transactions :" + ventes.size() + "", ""));


                } catch (IndexOutOfBoundsException e) {
                      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Numero Inexistant", ""));
 
                }

            } else {
                ventes = transDAO.findByNumero(id);
                List<Billet> list = billetDAO.findByProperty("numero", id);
                if (list.size() > 0) {
                    billet = list.get(0);
                } else {
                    billet = new Billet();
                }
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Recherche effectuée numero Billet normale:" + id + " Nombre transactions :" + ventes.size() + "", ""));
            }
        }
    }

    public Onligne getO() {
        return o;
    }

    public void setO(Onligne o) {
        this.o = o;
    }

    public String festivaleFromTypeWeb(int id) {
        try {
            Typeb b = ((Typeb) typebDAO.findByProperty("typeweb", id).get(0));
            return b.getEvenement().getTitre();
        } catch (IndexOutOfBoundsException boundsException) {
            return "Null ";
        }
    }

    public String TypeFromTypeWeb(int id) {
        Typeb b = ((Typeb) typebDAO.findByProperty("typeweb", id).get(0));
        return b.getTypeBillet().getModele();
    }

    public Billet getBillet() {
        return billet;
    }

    public void setBillet(Billet billet) {
        this.billet = billet;
    }

    public List<Billet> getLists() {
        return lists;
    }

    public void setLists(List<Billet> lists) {
        this.lists = lists;
    }

    public List<Trans_1> getVentes() {
        return ventes;
    }

    public void setVentes(List<Trans_1> ventes) {
        this.ventes = ventes;
    }

    public List<Kit> getKits() {
        return kits;
    }

    public void setKits(List<Kit> kits) {
        this.kits = kits;
    }
    private Vente selectedVenta;

    public Vente getSelectedVenta() {
        return selectedVenta;
    }

    public void setSelectedVenta(Vente selectedVenta) {
        this.selectedVenta = selectedVenta;
    }
}
